package com.ruoyi.system.controller;

import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.system.domain.CsInventoryTotalVo;
import com.ruoyi.system.domain.CsInventoryVo;
import com.ruoyi.system.domain.CsOrnamentName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.CsInventory;
import com.ruoyi.system.service.ICsInventoryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 库存物品Controller
 *
 * @author ruoyi
 * @date 2025-10-05
 */
@Controller
@RequestMapping("/system/inventory")
public class CsInventoryController extends BaseController {
    private String prefix = "system/inventory";

    @Autowired
    private ICsInventoryService csInventoryService;

    @GetMapping()
    public String inventory() {
        return prefix + "/inventory";
    }

    /**
     * 查询库存物品列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CsInventory csInventory) {
        startPage();
        List<CsInventoryVo> list = csInventoryService.selectCsInventoryList(csInventory);
        return getDataTable(list);
    }

    /**
     * 查询库存物品汇总数据
     */
    @PostMapping("/inventoryTotal")
    @ResponseBody
    public AjaxResult list(String name, Long account) {
        CsInventoryTotalVo csInventoryTotalVo = csInventoryService.selectCsInventoryTotal(name, account);
        return AjaxResult.success(csInventoryTotalVo);
    }

    /**
     * 导出库存物品列表
     */
    @Log(title = "库存物品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CsInventory csInventory) {
//        List<CsInventoryVo> list = csInventoryService.selectCsInventoryList(csInventory);
//        ExcelUtil<CsInventoryVo> util = new ExcelUtil<CsInventoryVo>(CsInventoryVo.class);
//        return util.exportExcel(list, "库存物品数据");
        return AjaxResult.success();
    }

    /**
     * 新增库存物品
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存库存物品
     */
    @Log(title = "库存物品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CsInventory csInventory) {
        return toAjax(csInventoryService.insertCsInventory(csInventory));
    }

    /**
     * 修改库存物品
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        CsInventory csInventory = csInventoryService.selectCsInventoryById(id);
        mmap.put("csInventory", csInventory);
        return prefix + "/edit";
    }

    /**
     * 修改保存库存物品
     */
    @Log(title = "库存物品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CsInventory csInventory) {
        return toAjax(csInventoryService.updateCsInventory(csInventory));
    }

    /**
     * 删除库存物品
     */
    @Log(title = "库存物品", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(csInventoryService.deleteCsInventoryByIds(ids));
    }

    /**
     * 查询饰品名称列表
     */
    @PostMapping("/getAvailableForSaleName")
    @ResponseBody
    public TableDataInfo getAvailableForSaleName(String name, Long account) {
        startPage();
        List<CsInventory> list = csInventoryService.getAvailableForSaleName(name, account);
        return getDataTable(list);
    }

}
